package com.bea.ftd.dashboard.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bea.ftd.common.ValidationException;
import com.bea.ftd.common.dao.SecretInfoRepo;
import com.bea.ftd.common.entity.SecretInfo;
import com.bea.ftd.common.enums.ValidationErrorCode;
import com.bea.ftd.dashboard.service.IGatewayService;
import com.bea.ftd.dashboard.service.ISecretInfoService;
import com.bea.ftd.dashboard.validator.BooleanValidator;
import com.bea.ftd.dashboard.validator.CustomTypeValidator;
import com.bea.ftd.dashboard.validator.NumberValidator;
import com.bea.ftd.dashboard.validator.StringValidator;

@Service
public class SecretInfoServiceImpl implements ISecretInfoService, IGatewayService {
	private Logger LOGGER = LoggerFactory.getLogger(SecretInfoServiceImpl.class);

	@Autowired
	private SecretInfoRepo secretInfoRepo;
	@Autowired
	private StringValidator stringValidator;
	@Autowired
	private BooleanValidator booleanValidator;
	@Autowired
	private NumberValidator numberValidator;
	@Autowired
	private CustomTypeValidator customTypeValidator;

	@Override
	public void addSecretInfo(SecretInfo secretInfo) throws ValidationException {

		validateInputParams(secretInfo);
		setCreateOrUpdateTime(secretInfo, secretInfoRepo);
		secretInfoRepo.save(secretInfo);

	}

	/**
	 * 验证输入参数是否有效
	 * 
	 * @param secretInfo
	 * @throws ValidationException
	 */
	private void validateInputParams(SecretInfo secretInfo) throws ValidationException {
		if (stringValidator.isEmptyString(secretInfo.getPartnerCode())) {
			throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "合作商户号不能为空");
		}

		if (!customTypeValidator.isValidEncrytType(secretInfo.getEncryptType())) {
			throw new ValidationException(ValidationErrorCode.INVALID_TYPE, "无效的加密类型");
		}

		if (!booleanValidator.isBooleanType(secretInfo.getIsEncryptHardware())) {
			throw new ValidationException(ValidationErrorCode.INVALID_BOOLEAN_VALUE, "无效的isEncryptHardware值");
		}


		if (1 == secretInfo.getIsEncryptHardware()) {
			if (!stringValidator.isValidIp(secretInfo.getEncryptHardwareIp())) {
				throw new ValidationException(ValidationErrorCode.INVALID_IP, "硬加密需要填入有效的ip值");
			}

			if (stringValidator.isEmptyString(secretInfo.getPrivateKeyPath())) {
				throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "硬加密私钥路径不能为空");
			}

			if (stringValidator.isEmptyString(secretInfo.getPublicKeyPath())) {
				throw new ValidationException(ValidationErrorCode.EMPTY_VALUE, "硬加密公钥路径不能为空");
			}
			
			if(!numberValidator.isValidPort(secretInfo.getEncryptHardwarePort())) {
				throw new ValidationException(ValidationErrorCode.INVALID_PORT, "无效的端口值");
			}
		}

		if (!booleanValidator.isBooleanType(secretInfo.getIsActivated())) {
			throw new ValidationException(ValidationErrorCode.INVALID_BOOLEAN_VALUE, "无效的isActivated值");
		}
		
	}

	@Override
	public List<SecretInfo> getAllSecretInfos() {
		try {
			return secretInfoRepo.findAll();
		} catch (Exception e) {
			LOGGER.error("error when querying secretInfo from database " + e.getMessage());
			return null;
		}
	}
}
